package dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

import dashboard.databll.IncomeBLL;
import dashboard.utils.MyServletRequestUtils;

@Path("/income/ajax")
public class IncomeAjaxController extends MvcController {

	/**
	 * 获取城市-产品类型[精准，置顶，智能]数据
	 * @return
	 */
	@Path("/cityProduct")
	@POST
	public ActionResult getCityProData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
//			EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeBLL.getCityProJson(dateGroup,stat_type,businessName,cityName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = IncomeBLL.getCityProTableJson(dateGroup,stat_type,businessName,dateVal);
			}
			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	
	/**
	 * 获取某个产品类型精准/置顶/智能的 两个城市对比数据
	 * @return
	 */
	@Path("/cityCmp")
	@POST
	public ActionResult getCityCmpData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			//产品类型，精准/置顶/智能
			String proType = MyServletRequestUtils.getStringParameter(request, "proType","jingzhun");
			
			
			String cityName1 = MyServletRequestUtils.getStringParameter(request, "city1","北京");
			String cityName2 = MyServletRequestUtils.getStringParameter(request, "city2","天津");

//			EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = IncomeBLL.getCityCmpJson(dateGroup,stat_type,businessName,proType,cityName1,cityName2);
			log.info(businessName+"--"+cityName1+"-"+cityName2+"--"+proType+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取城市-消耗周期数据
	 * @return
	 */
	@Path("/cityConsume")
	@POST
	public ActionResult getCityConsumeData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String consume_citys = MyServletRequestUtils.getStringParameter(request, "consume_citys", "北京");
			String index1 = MyServletRequestUtils.getStringParameter(request, "index1", "xhzq");
			String index2 = MyServletRequestUtils.getStringParameter(request, "index2", "xiaohao");
			
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeBLL.getCityConsumeJson(dateGroup,stat_type,businessName,consume_citys,index1,index2);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = IncomeBLL.getCityConsumeTableJson(dateGroup,stat_type,businessName,dateVal);
			}
			log.info(businessName+"--"+consume_citys+"--"+index1+"--"+index2+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
}
